package com.kaibes.admin.account;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.kaibes.admin.account.email.MailTemplateInsert;
import com.kaibes.admin.account.email.MailTemplateService;
import com.kaibes.core.base.util.ObjectUtils;
import com.kaibes.core.spring.proxy.Proxy;
import com.kaibes.module.account.core.AccountRes;
import com.kaibes.web.thymeleaf.ThymeleafConfig;
import com.kaibes.web.thymeleaf.Variable;

@Configuration
public class AccountInit implements InitializingBean {

    @Proxy(false)
    private MailTemplateService mailTemplateService;
    @Autowired
    private ThymeleafConfig thymeleafConfig;
    
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
	@Override
	public void afterPropertiesSet() throws Exception {
		if (mailTemplateService == null) {
    		return;
    	}
    	
        List<Variable> variables = new ArrayList<>(thymeleafConfig.getBaseVariables());
        Variable variable = new Variable();
        variable.setName("邮箱验证码");
        variable.setValue(AccountRes.RESET_PASSWORD_CODE);
        variables.add(variable);
        
        Object template = mailTemplateService.getByKey(AccountRes.RESET_PASSWORD_TEMPLATE);
        if (template == null) {
            MailTemplateInsert data = new MailTemplateInsert();
            data.setKey(AccountRes.RESET_PASSWORD_TEMPLATE);
            data.setRemark("当申请重置账号密码的时候，会发送这封邮件");
            data.setTitle("<th:block th:utext=\"${siteInfo.title}\">网站标题</th:block>");
            data.setEmailContent("亲爱的用户 <font color=\"blue\" th:utext=\"${users.get(email).nickname}\">收件人</font>\n"
                    + "\n"
                    + "&emsp;&emsp;您好，您申请了邮箱验证码，用于<font color=\"red\"\n"
                    + "size=\"4\">重置您的账号密码</font>。如果您没有做过这个操作，或者已经不再需要了，可以直接忽略这封邮件。\n"
                    + "\n"
                    + "&emsp;&emsp;邮箱验证码如下：\n"
                    + "&emsp;&emsp;<font color=\"blue\" th:utext=\"${resetPasswordCode}\">邮箱验证码</font>\n"
                    + "\n"
                    + "&emsp;&emsp;请勿泄漏您的验证码，以免造成损失。\n"
                    + "\n"
                    + "此致敬礼！\n"
                    + "<font color=\"blue\" th:utext=\"${dateTime2}\">当前时间(中文)</font>\n"
                    + "\n"
                    + "");
            data.setHtml("<p>亲爱的用户 <font color=\"blue\" th:utext=\"${users.get(email).nickname}\">收件人</font></p>\n"
                    + "<p>  您好，您申请了邮箱验证码，用于<font color=\"red\" size=\"4\">重置您的账号密码</font>。如果您没有做过这个操作，或者已经不再需要了，可以直接忽略这封邮件。</p>\n"
                    + "<p>  邮箱验证码如下：<br />\n"
                    + "  <font color=\"blue\" th:utext=\"${resetPasswordCode}\">邮箱验证码</font></p>\n"
                    + "<p>  请勿泄漏您的验证码，以免造成损失。</p>\n"
                    + "<p>此致敬礼！<br />\n"
                    + "<font color=\"blue\" th:utext=\"${dateTime2}\">当前时间(中文)</font></p>\n"
                    + "");
            data.setVariables(variables);
            mailTemplateService.save(data);
        } else {
            MailTemplateInsert data = new MailTemplateInsert();
            data.setVariables(variables);
			try {
				String key = ObjectUtils.getField(template, "key");
				mailTemplateService.updateByKey(data, key);
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
        }
	}
    
}
